//
//  SettingViewController.m
//  项目三
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 rimi. All rights reserved.
// 440 377

#import "SettingViewController.h"

static NSTimeInterval const duration = 0.7f;

@interface SettingViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
    
    UIImagePickerController *_imagePickerController; /**< 图片拾取器 */
    UIView *_headView;
}
/* ----------- mainView -------------*/
@property (strong, nonatomic) IBOutlet UIView *mainView;

/* ----------- userView -------------*/
@property (strong, nonatomic) IBOutlet UIView *userNameChangeView;
@property (strong, nonatomic) IBOutlet UILabel *userName;
@property (strong, nonatomic) IBOutlet UILabel *phone;

@property (strong, nonatomic) IBOutlet UITextField *newuserField;
@property (strong, nonatomic) IBOutlet UITextField *newuserAgainField;
@property (strong, nonatomic) IBOutlet UITextField *userCode;

/* ----------- passView -------------*/
@property (strong, nonatomic) IBOutlet UIView *userpassWordChangeView;
@property (strong, nonatomic) IBOutlet UILabel *passPhone;
@property (strong, nonatomic) IBOutlet UITextField *oldPassField;
@property (strong, nonatomic) IBOutlet UITextField *passNewField;
@property (strong, nonatomic) IBOutlet UITextField *passNewAgainField;
@property (strong, nonatomic) IBOutlet UITextField *passCode;

/* ----------- headView -------------*/
@property (strong, nonatomic) IBOutlet UIView *headChangeView;

@property (strong, nonatomic) IBOutlet UIImageView *headImageView;

- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}

- (void)initializeUserInterface {
    
    self.titleImageView.image = [UIImage imageNamed:@"设置logo"];
    [super initializeUserInterface];
    self.titleImageView.frame = DHFlexibleFrame(CGRectMake(20, 20, 88, 37));
    
    _imagePickerController = [[UIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
}
#pragma mark - privte methods
/* ----------- userView -------------*/
- (IBAction)userChangeEvent:(id)sender {
    [UIView animateWithDuration:duration animations:^{
        self.mainView.alpha = 0;
    } completion:^(BOOL finished) {
        self.userNameChangeView.alpha = 1;
    }];
}

- (IBAction)passwordChangeEvent:(id)sender {
    
    [UIView animateWithDuration:duration animations:^{
        self.mainView.alpha = 0;
    } completion:^(BOOL finished) {
        self.userpassWordChangeView.alpha = 1;
    }];
}

- (IBAction)changeHeadEvent:(id)sender {
    
    [UIView animateWithDuration:duration animations:^{
        self.mainView.alpha = 0;
    } completion:^(BOOL finished) {
        self.headChangeView.alpha = 1;
    }];
}

- (IBAction)clearEvent:(id)sender {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
        NSLog(@"files :%ld",(unsigned long)[files count]);
        for (NSString *p in files) {
            NSError *error;
            NSString *path = [cachPath stringByAppendingPathComponent:p];
            if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
            }
        }
        [self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];
    });
}
- (void)clearCacheSuccess {
    NSLog(@"清理成功");
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"清理成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}
/* ----------- userView -------------*/
- (IBAction)sendUserCode:(id)sender {
    
}
- (IBAction)userYes:(id)sender {
    
}

- (IBAction)userBack:(id)sender {
    [UIView animateWithDuration:duration animations:^{
        self.userNameChangeView.alpha = 0;
    } completion:^(BOOL finished) {
        self.mainView.alpha = 1;
    }];
}


/* ----------- passView -------------*/
- (IBAction)sendPassCode:(id)sender {
    
}
- (IBAction)passYes:(id)sender {
    
}

- (IBAction)passBack:(id)sender {
    [UIView animateWithDuration:duration animations:^{
        self.userpassWordChangeView.alpha = 0;
    } completion:^(BOOL finished) {
        self.mainView.alpha = 1;
    }];
}

/* ----------- codeView -------------*/
- (IBAction)choseHead:(id)sender {
    
    [self readPhoto];
}

- (void)readPhoto {
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    // 判断是否支持需要设置的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        
        // 1.设置图片拾取器上的sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 2.设置支持的媒体格式
        _imagePickerController.mediaTypes = @[mediaTypes[0], mediaTypes[1]];
        // 3.其他设置
        _imagePickerController.allowsEditing = YES;
        // 4.推送图图片拾取器控制器
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    }
}
#pragma mark - <UIImagePickerControllerDelegate> & <UINavigationControllerDelegate>

// 用户选择了某个媒体
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"User chosed imageView media with info '%@'.", info);
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        if ([info[UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {
            UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
            self.headImageView.image = originalImage;
        }
    }else if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
        UIImage *editedImage = info[@"UIImagePickerControllerEditedImage"];
        UIImage *image = info[UIImagePickerControllerEditedImage];
        image = editedImage;
        self.headImageView.image = editedImage;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)systemHead:(id)sender {
    UIButton *btn = (UIButton *)sender;
    
    _headView = [[UIView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) - 105, CGRectGetMaxY(btn.frame) + 180, 440, 377))];
    _headView.backgroundColor = [UIColor clearColor];
    _headView.hidden = NO;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:_headView.bounds];
    imageView.image = [UIImage imageNamed:@"设置 头像 小背景"];
    [_headView addSubview:imageView];
    
    for (int i = 0; i < 18; i ++) {
        UIButton *headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [headBtn setFrame:DHFlexibleFrame(CGRectMake(25 + i % 6 * 65, 53 + i / 6* 108, 55, 55))];
        [headBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"头像%d",i + 1]] forState:UIControlStateNormal];
        [headBtn addTarget:self action:@selector(respondsToHeadBtn:) forControlEvents:UIControlEventTouchUpInside];
        headBtn.tag = i + 100;
        [_headView addSubview:headBtn];
    }
    [self.view addSubview:_headView];
}
- (void)respondsToHeadBtn:(UIButton *)sender {

    NSInteger index = sender.tag - 100;
    _headView.hidden = YES;
    self.headImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"头像%d",index + 1]];
}
- (IBAction)takePhoto:(id)sender {
    [self takePictures];
}
- (void)takePictures {
    
    NSArray * mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    // 判断是否支持需要设置的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        _imagePickerController.mediaTypes = @[mediaTypes[0]];
        // 相机模式
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        // 相机设别前置摄像头还是后置摄像头
        _imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        // 相机闪光灯模式
        _imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    } else {
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"找不到相机设备" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertController animated:YES completion:nil];
    }
}
- (IBAction)headYes:(id)sender {
    
}

- (IBAction)headBack:(id)sender {
    [UIView animateWithDuration:duration animations:^{
        self.headChangeView.alpha = 0;
    } completion:^(BOOL finished) {
        self.mainView.alpha = 1;
    }];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    _headView.hidden = YES;
    
}



@end
